package com.han.shadow.customerapp.fragment;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.han.shadow.customerapp.R;
import com.han.shadow.customerapp.adapter.MyGoodsesAdapter;
import com.han.shadow.customerapp.bean.Goods;
import com.han.shadow.customerapp.http.Connector;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ACE on 2016/4/22 0022.
 */
public class GoodsListFragment extends Fragment {


    private String busID;
    private View view = null;
    private ListView goodsList = null;

    private Connector conn = new Connector();
    private List<Goods> goodses = new ArrayList<>();
    private MyGoodsesAdapter adapter = null;

    private Context parent = null;
    private LayoutInflater inflater = null;
    private final Handler handlerParent;

    private List<List<Bitmap>> bitmaps = new ArrayList<>();


    public GoodsListFragment(Context context, LayoutInflater inflater, String busID,Handler handler){
        this.parent = context;
        this.inflater = inflater;
        this.busID = busID;
        this.handlerParent = handler;
    }

    public List<Goods> getGoodses(){
        return goodses;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_goodslist,null);
        goodsList = (ListView)view.findViewById(R.id.goodsList);


        goodsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Goods goods = goodses.get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("goods", goods);
                bundle.putSerializable("bitmap",(Serializable)bitmaps.get(position));
                Message msg = new Message();
                msg.setData(bundle);
                msg.what = 2;//打开商品信息
                handlerParent.sendMessage(msg);
            }
        });

//        goodsList.setOnScrollChangeListener(new View.OnScrollChangeListener() {
//            @Override
//            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//                updateImage();
//            }
//        });

//        goodsList.setOnScrollListener(new AbsListView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(AbsListView view, int scrollState) {
//
//            }
//
//            @Override
//            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//                updateImage();
//            }
//        });

        getGoods();
        return view;
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){//更新商品list
                if(goodses != null) {
                    adapter = new MyGoodsesAdapter(goodses, inflater, parent);
                    goodsList.setAdapter(adapter);
                    getPic();
                }
            }else if(msg.what == 2){//更新图片
                //adapter.update(bitmaps);
                updateImage();

            }
        }
    };


    public void updateImage(){
        int  visiblePostion = goodsList.getFirstVisiblePosition();
        int visibleLastPostion = goodsList.getLastVisiblePosition();
        for(int i  = 0; i <= visibleLastPostion; i++){
            View view = goodsList.getChildAt(i-visiblePostion);

            ImageView iv = (ImageView)view.findViewById(R.id.image_gs_goods);
            iv.setImageBitmap(bitmaps.get(i).get(0));
        }
    }
    public void getGoods(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                goodses = conn.queryGoods(null,busID);
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        }).start();
    }

    public void getPic(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(Goods goods:goodses){
                    List<Bitmap> bs = new ArrayList<Bitmap>();
                    for(String pic:goods.getPicPath()){
                        bs.add(conn.queryPic(pic));
                    }
                    bitmaps.add(bs);
                }
                Message msg = new Message();
                msg.what = 2;
                handler.sendMessage(msg);

            }
        }).start();
    }

}
